package top.abilities.util.utils.Excel;



import top.abilities.util.util.DESUtil;
import top.abilities.util.util.MD5Util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 流操作类
 *
 * @author dong
 * @create 2018-07-04 11:39
 **/
public class StreamUtil {
    public static final byte[] input2byte(InputStream inStream)
            throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }
    public static final InputStream byte2Input(byte[] buf) {
        return new ByteArrayInputStream(buf);
    }

    private static final String Key= MD5Util.MD5("dong!(*&").substring(0,8).toUpperCase();
    public static InputStream enCrypt(InputStream inputStream) throws IOException {
        byte[] bytes= StreamUtil.input2byte(inputStream);
        return new ByteArrayInputStream(DESUtil.encryptCBC(bytes,Key.getBytes("utf-8"),Key.getBytes("utf-8")));
    }
    public static ByteArrayOutputStream deCrypt(byte[] bytes) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        swapStream.write(DESUtil.decryptCBC(bytes,Key.getBytes("utf-8"),Key.getBytes("utf-8")));
        return swapStream;
    }
}
